package com.yuxl.common.core.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuxl.common.core.handler.ApiException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Author : yuxl
 * @create 2025/7/11 下午4:34
 */
@Slf4j
@Component
public class ObjectUtil {

    @Resource
    private ObjectMapper objectMapper;

    public String writeValueAsString(Object value){
        try {
            return objectMapper.writeValueAsString(value);
        }catch (Exception e){
            log.error("objectMapper writeValueAsString error：{}",e.getMessage(),e);
            throw new ApiException("objectMapper writeValueAsString error");
        }
    }

    public <T> T readValue(String content, Class<T> valueType){
        try {
            return objectMapper.readValue(content,valueType);
        }catch (Exception e){
            log.error("objectMapper readValue error：{}",e.getMessage(),e);
            throw new ApiException("objectMapper readValue error");
        }
    }
}
